import request from '@/utils/request'
import { BaseResponse } from '@/types/api'

// 角色相关接口的类型定义
export interface Role {
  id: number;
  name: string;
  keyword: string;
  desc?: string;
  status: number;
  sort: number;
  createdAt?: string;
  updatedAt?: string;
}

export interface RoleListParams {
  page?: number;
  pageSize?: number;
  name?: string;
  keyword?: string;
  status?: number | string;
}

export interface RoleData {
  name: string;
  keyword: string;
  desc?: string;
  status: number;
  sort: number;
}

export interface RoleMenus {
  menus: number[];
}

export interface RoleApis {
  apis: number[];
}

// 获取角色列表
export function getRoles(params: RoleListParams): Promise<BaseResponse<{ list: Role[], total: number }>> {
  return request({
    url: '/role/list',
    method: 'get',
    params
  })
}

// 创建角色
export function createRole(data: RoleData): Promise<BaseResponse> {
  return request({
    url: '/role/create',
    method: 'post',
    data
  })
}

// 更新角色
export function updateRoleById(roleId: number, data: Partial<RoleData>): Promise<BaseResponse> {
  return request({
    url: '/role/update/' + roleId,
    method: 'patch',
    data
  })
}

// 获取角色的权限菜单
export function getRoleMenusById(roleId: number): Promise<BaseResponse<RoleMenus>> {
  return request({
    url: '/role/menus/get/' + roleId,
    method: 'get'
  })
}

// 更新角色的权限菜单
export function updateRoleMenusById(roleId: number, data: RoleMenus): Promise<BaseResponse> {
  return request({
    url: '/role/menus/update/' + roleId,
    method: 'patch',
    data
  })
}

// 获取角色的权限接口
export function getRoleApisById(roleId: number): Promise<BaseResponse<RoleApis>> {
  return request({
    url: '/apis/get/' + roleId,
    method: 'get'
  })
}

// 更新角色的权限接口
export function updateRoleApisById(roleId: number, data: RoleApis): Promise<BaseResponse> {
  return request({
    url: '/apis/update/' + roleId,
    method: 'patch',
    data
  })
}

// 批量删除角色
export function batchDeleteRoleByIds(data: { roleIds: number[] }): Promise<BaseResponse> {
  return request({
    url: '/role/delete/batch',
    method: 'delete',
    data
  })
}
